home *** CD-ROM | disk | FTP | other *** search
- unit RawData;
-
- {$ifndef Win32}
- 'This is for Delphi 2/Win32 only'
- {$endif}
-
- interface
-
- // RawDataToPrinter - sends binary data directly to a printer
- //
- // Params:
- // PrinterName - printer name
- // Data - raw data bytes - any variable will do
- // Count - length of Data in bytes
- //
- // Gives an exception upon failure
- // (with Break on Exception on it may give several)
- procedure RawDataToPrinter(PrinterName: String; const Data; Count: Integer);
-
- implementation
-
- uses
- WinSpool, Windows, Consts, SysUtils;
-
- type
- EPrinterError = class(Exception);
-
- procedure Error;
- begin
- raise EPrinterError.CreateRes(SInvalidPrinterOp);
- end;
-
- procedure RawDataToPrinter(PrinterName: String; const Data; Count: Integer);
- type
- TDoc_Info_1 = record
- DocName,
- OutputFile,
- Datatype: PChar;
- end;
- var
- hPrinter: THandle;
- DocInfo: TDoc_Info_1;
- BytesWritten: Integer;
- begin
- // Need a handle to the printer
- if not OpenPrinter(PChar(PrinterName), hPrinter, nil) then
- Error;
- // Fill in the structure with info about this "document"
- DocInfo.DocName := 'My Document';
- DocInfo.OutputFile := nil;
- DocInfo.Datatype := 'RAW';
- try
- // Inform the spooler the document is beginning
- if StartDocPrinter(hPrinter, 1, @DocInfo) = 0 then
- Error;
- try
- // Start a page
- if not StartPagePrinter(hPrinter) then
- Error;
- try
- // Send the data to the printer
- if not WritePrinter(hPrinter, @Data, Count, BytesWritten) then
- Error;
- finally
- // End the page
- if not EndPagePrinter(hPrinter) then
- Error;
- end;
- finally
- // Inform the spooler that the document is ending
- if not EndDocPrinter(hPrinter) then
- Error;
- end;
- finally
- // Tidy up the printer handle
- ClosePrinter(hPrinter);
- end;
- // Check to see if correct number of bytes written
- if BytesWritten <> Count then
- Error;
- end;
-
- end.
-